package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SportsDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sports;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author houzongtao
 * @date 2020/9/1 14:53
 */
@Service(interfaceClass = SportsService.class)
@Transactional
public class SportsServiceImpl implements SportsService {
    @Autowired
    private SportsDao sportsDao;

    //删除
    public void delete(Integer id){
        sportsDao.deleteById(id);
    }

    //编辑
    public void edit(Sports sports) {
        sportsDao.edit(sports);
    }

    public Sports findById(Integer id) {
        return sportsDao.findById(id);
    }

    //分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Sports> page = sportsDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新建
    public void add(Sports sports) {
        sportsDao.add(sports);
    }

    //批量导入
    public void batchAdd(List<Sports> sportsList) {
        if(sportsList != null && sportsList.size() > 0){
            for (Sports sports : sportsList) {
                //检查此数据（Id）是否存在
                long count = sportsDao.findCountById(sports.getId());
                if(count > 0){
                    //已经存在，执行更新操作
                    sportsDao.editSportsById(sports);
                }else{
                    //不存在，执行添加操作
                    sportsDao.add(sports);
                }
            }
        }
    }
}
